﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Blam_Unity.Classes.Windows_Explorer
{
    class checkFileInfo
    {
        /// <summary>
        /// Check the Magic of the selected file
        /// </summary>
        /// <param name="mio">Mojos IO already declared</param>
        /// <returns></returns>
        public static string checkMagic(MojosIO mio)
        {
            mio.Position = 0x00;
            string magic = mio.ReadString(0x04, Encoding.ASCII);

            return magic;
        }

        /// <summary>
        /// Check the title of the first package in a CON file
        /// </summary>
        /// <param name="mio">Mojos IO already declared</param>
        /// <returns></returns>
        public static string checkCONType(MojosIO mio)
        {
            mio.Position = 0xC000;
            string CONPackageType = mio.ReadString(0x25, Encoding.ASCII);

            return CONPackageType;
        }

        /// <summary>
        /// Check the XEX's titleID
        /// </summary>
        /// <param name="mio">Mojos IO already declared</param>
        /// <returns></returns>
        public static string checkXEXID(MojosIO mio)
        {
            string xexID;

            if (mio.StreamLength < 14000000)
            {
                mio.Position = 0x2CEC;
                byte[] fileID = mio.ReadBytes(0x04);
                xexID = BitConverter.ToString(fileID).Replace("-", "");
            }
            else
            {
                mio.Position = 0x2CAC;
                byte[] fileID = mio.ReadBytes(0x04);
                xexID = BitConverter.ToString(fileID).Replace("-", "");
            }

            return xexID;
        }

        /// <summary>
        /// Check what type of BLF it is
        /// </summary>
        /// <param name="mio">Mojos IO already declared</param>
        /// <returns></returns>
        public static string checkBLFtype(MojosIO mio)
        {
            mio.Position = 0x30;
            return mio.ReadString(0x04, Encoding.ASCII);
        }

        /// <summary>
        /// Check to see if the current XEX is compressed
        /// </summary>
        /// <param name="mio">Mojos IO already declared</param>
        /// <returns></returns>
        public static bool checkXEXCompression(MojosIO mio)
        {
            bool isDecompressed;

            if (mio.StreamLength < 14000000)
            {
                isDecompressed = false;
            }
            else
            {
                isDecompressed = true;
            }

            return isDecompressed;
        }

        /// <summary>
        /// Get details of the selected file
        /// </summary>
        /// <param name="fileName">Windows File System Strucutre path</param>
        /// <returns></returns>
        public static string getFileDetails(string fileName)
        {
            FileInfo fi = new FileInfo(fileName);

            double fileSizeMB = Convert.ToDouble(fi.Length);

            string fileInfo = "File Direcctory: " + fi.Directory + "\\\n";
            fileInfo += "File Size: " + (fileSizeMB / 1024 / 1024) + " MB's (" + fi.Length + " bytes)\n";
            fileInfo += "File Name: " + fi.Name + "\n";
            fileInfo += "Last Read Time: " + fi.LastAccessTimeUtc + "\n";
            fileInfo += "Last Write Time: " + fi.LastWriteTimeUtc + "\n";

            return fileInfo;
        }
    }
}